﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.SignalR;

namespace SignalRDemo.Connections
{
    public class ChatConnection : PersistentConnection
    {
        private static int _connections = 0;
        protected override Task OnConnected(IRequest request, string connectionId)
        {
            Interlocked.Increment(ref _connections);

            Connection.Broadcast($"新的连接加入，连接ID：{connectionId},已有连接数：{_connections}");
            return Connection.Send(connectionId, "Welcome!");
        }

        protected override Task OnDisconnected(IRequest request, string connectionId, bool stopCalled)
        {
            Interlocked.Decrement(ref _connections);

            return Connection.Broadcast($"{connectionId} 断开了连接, ,已有连接数：{_connections}");
        }

        protected override Task OnReceived(IRequest request, string connectionId, string data)
        {
            var message = $"{connectionId}发送内容>>{data}";
            return Connection.Broadcast(data);
        }
        
    }
}